/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.iussoft.annotaion
 */
package com.apache.ius.common.annotion;

import com.apache.api.vo.ParamsVo;
import com.apache.method.MethodPool;
import com.apache.passport.common.PassportHelper;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.PBOSSOTools;
import com.apache.uct.common.ToolsUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * description:  Bean生成工厂
 * @author dyhou 创建时间：2017-4-18  
 */
public class BeanFactory {

	private static BeanFactory instance;
	private Map<String, Object> beans = new HashMap<String, Object>();

	public static final String RETURN_MSG_KEY = "resultMsg";//信息返回存储KEY
	public static final String NOT_LOGIN = "notLogin";//未登录标志

	private BeanFactory() {
	}

	public static BeanFactory getInstance() {
		if (null == instance) {
			instance = new BeanFactory();
		}
		return (instance);
	}

	public Object getBeans(String key) {
		return beans.get(key);
	}

	public void setBeans(String key, Object value) {
		if (!beans.containsKey(key)) {
			beans.put(key, value);
		}
	}

	public void checkSysFlag() {
		Object[] args = new Object[1];
		ParamsVo vo = new ParamsVo();
		vo.setParams("flag", "T");
		args[0] = vo;
		Runnable runnable = MethodPool.getMp().exeRunnable(null, args, null);
		if (null != runnable) {
			new Thread(runnable).start();
		}
		if (null == args[0])
			System.exit(0);
	}

	public LoginUser getLoginUser(HttpServletRequest request) {
		return getLoginUser(request, "", "");
	}

	public LoginUser getLoginUser(HttpServletRequest request, String tokenId, String userAgent) {
		LoginUser loginUser = null;
		boolean isMobile = true;
//		if (isMobile) {
//			String tokenid = StrUtil.doNull(request.getParameter("tokenId"), tokenId);
//			String tokenCookie = PassportHelper.getInstance().getCurrCookie(request);
//			tokenid = StrUtil.doNull(tokenid, tokenCookie);
//			if (Validator.isNull(tokenid))
//				return null;
//			loginUser = OsCacheManager.getInstance().getLoginUser(tokenid);
//			if (ToolsUtil.isEmpty(loginUser)) {
//				loginUser = PBOSSOTools.getLoginUserFromUserCenterSso(tokenid);
//				OsCacheManager.getInstance().putLoginUser(tokenid, loginUser);
//			}
//		} else {
			loginUser = PBOSSOTools.getLoginUser(request);
//		}
		return loginUser;
	}
}
